/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.service.document;

import com.documents.htmlobj.CatTreeObj;
import com.documents.htmlobj.ProductListObj;
import com.documents.htmlobj.ProductObj;
import com.documents.pojo.DocumentGroup;
import com.documents.pojo.Product;
import com.documents.service.product.ProductService;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Administrator
 */
public class DocumentIndexTreeService {

    private DocumentTypeService documentTypeService;
    private DocumentGroupService documentGroupService;
    private ProductService productService;
    private List<CatTreeObj> tree;
    private List<ProductListObj> list;

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public DocumentIndexTreeService() {
    }

    public DocumentGroupService getDocumentGroupService() {
        return documentGroupService;
    }

    public void setDocumentGroupService(DocumentGroupService documentGroupService) {
        this.documentGroupService = documentGroupService;
    }

    public DocumentTypeService getDocumentTypeService() {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService) {
        this.documentTypeService = documentTypeService;
    }

    public List<CatTreeObj> createTree() {
        tree = new ArrayList<CatTreeObj>();
        List<DocumentGroup> rootgroups = this.documentGroupService.find("pid=?", new Object[]{1}, "code");
        if (rootgroups != null) {
            for (DocumentGroup dg : rootgroups) {

                CatTreeObj cto = new CatTreeObj();
                cto.setGroup(dg);
                cto.setLevel(1);
                Set types = dg.getDocumentTypes();
                List<DocumentGroup> level2 = this.documentGroupService.find("pid=?", new Object[]{dg.getId()}, "code");
                if (level2 != null && types.isEmpty()) {
                    List<CatTreeObj> tmplist = new ArrayList<CatTreeObj>();
                    for (DocumentGroup dg2 : level2) {
                        CatTreeObj cto2 = new CatTreeObj();
                        cto2.setGroup(dg2);
                        cto2.setLevel(2);
//                        List pplist = documentTypeService.find("id=?", new Object[]{cto2.getGroup().getId()}, "code");
//                        pplist.
                        Set types2 = dg2.getDocumentTypes();
                        if (!types2.isEmpty()) {
                            cto2.setTypelist(new ArrayList(types2));
                        }
                        tmplist.add(cto2);

                    }
                    cto.setCatTreeObjlist(tmplist);
                } else {
                    cto.setTypelist(new ArrayList(types));
                    tree.add(cto);
                    continue;
                }
                tree.add(cto);
            }
        }

        return tree;
    }

    public List<ProductObj> createList() {

        List<Product> root = this.productService.find("pid=?", new Object[]{1}, "code");
        List<ProductObj> l = new ArrayList<ProductObj>();
        if (root != null) {

            for (Product p : root) {
                ProductObj o = new ProductObj();
                o.setId(p.getId());
                o.setLevel(1);
                o.setName(p.getAlias());
                List<Product> level2 = this.productService.find("pid=?", new Object[]{o.getId()}, "code");
                if (level2 != null && level2.size() != 0) {
                    List<ProductObj> l2 = new ArrayList<ProductObj>();
                    for (Product p2 : level2) {
                        ProductObj o2 = new ProductObj();
                        o2.setId(p2.getId());
                        o2.setLevel(2);
                        o2.setName(p2.getAlias());
                        List<Product> level3 = this.productService.find("pid=?", new Object[]{o2.getId()}, "code");
                        if (level3 != null && level3.size() != 0) {
                            List<ProductObj> l3 = new ArrayList<ProductObj>();
                            for (Product p3 : level3) {

                                ProductObj o3 = new ProductObj();
                                o3.setId(p3.getId());
                                o3.setLevel(3);
                                o3.setName(p3.getAlias());
                                l3.add(o3);
                            }
                            o2.setProduceObjlist(l3);
                            l2.add(o2);
                        } else {
                            l2.add(o2);
                        }

                    }
                    o.setProduceObjlist(l2);
                    l.add(o);
                } else {

                    l.add(o);
                    continue;
                }

            }
        }

        return l;
    }
}
